<template>
  <!-- 检查事项 -->
  <div class="dialog-container">
    <el-dialog
      :title="dialogType==='add'?'添加停产报告单':dialogType === 'detail'?'停产报告详情':'修改停产报告单'"
      :visible="dialogVisible"
      append-to-body
      @open="initData"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        :rules="rules"
        label-width="110px"
        size="mini"
        :disabled="dialogType === 'detail'"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="停产开始日期" prop="startTime">
              <el-date-picker v-model="dataForm.startTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择时间" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="停产结束日期" prop="endTime">
              <el-date-picker v-model="dataForm.endTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择时间" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="dataForm.remark" type="textarea" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="4">
            <el-form-item label="停产报告" prop="stopProductionReport">
              <UploadImage
                :url="dataForm.stopProductionReport"
                @setUrl="setUrl"
              />
            </el-form-item>
          </el-col> -->
          <el-col :span="24" label="停产报告" prop="material">
            <el-form-item label="停产报告" prop="material">
              <UploadFile
                ref="uploadFile"
                :files="dataForm.stopProductionReport"
                :dialog-type="dialogType"
              />
            </el-form-item>
          </el-col>
          <iframe v-if="dataForm.stopProductionReport" :src="dataForm.stopProductionReport" frameborder="0" style="width: 100%; height: 450px" />
        </el-row>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" class="sure-btn" size="mini" type="primary" @click="handleSave">保存</el-button>
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, edit } from '@/api/base/stopProduction'
import UploadImage from '@/components/UploadImage/index'
import UploadFile from '@/components/UploadFile'

export default {
  name: 'StopDialog',
  components: { UploadImage, UploadFile },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    addUpdateObj: {
      type: Object,
      default: null
    },
    dialogType: {
      type: String,
      default: 'detail'
    }
  },
  data() {
    return {
      saveLoading: false,
      dataForm: {
        id: null,
        startTime: '',
        endTime: '',
        stopProductionReport: null,
        outletId: null,
        remark: null
      },
      rules: {
        // examineResult: [{ required: true, message: '请选择违法行为判定', trigger: 'change' }]
      }
    }
  },
  created() {
    this.clearData()
  },
  methods: {
    initData() {
      this.dataForm = this.addUpdateObj
      debugger
      if (this.addUpdateObj && this.addUpdateObj.stopProductionReport) {
        const json = JSON.parse(this.addUpdateObj.stopProductionReport)
        this.addUpdateObj.stopProductionReport = json[0].url
        const type = this.addUpdateObj.stopProductionReport.substring(this.addUpdateObj.stopProductionReport.lastIndexOf('.') + 1)
        this.addUpdateObj.stopProductionReport = 'http://47.108.114.62:8080/static-view' + this.addUpdateObj.stopProductionReport
        if (type === 'docx') {
          this.addUpdateObj.stopProductionReport = 'http://view.xdocin.com/xdoc?_xdoc=' + this.addUpdateObj.stopProductionReport
        }
      }
    },
    // setUrl(url) {
    //   this.dataForm.stopProductionReport = url
    // },
    handleSave() {
      const files = this.$refs.uploadFile.getFiles()
      if (files) {
        this.dataForm.stopProductionReport = JSON.stringify(files)
      }
      if (this.dataForm.id) {
        this.saveLoading = true
        edit(this.dataForm).then(() => {
          this.$message.success('成功')
          this.$emit('flushPage')
          this.$emit('closeDialog')
          this.clearData()
          this.saveLoading = false
        }).catch(() => {
          this.saveLoading = false
        })
      } else {
        if (this.addUpdateObj) {
          this.saveLoading = true
          this.dataForm.outletId = this.addUpdateObj.outletId
          debugger
          add(this.dataForm).then(() => {
            debugger
            this.$message.success('成功')
            this.$emit('flushPage')
            this.$emit('closeDialog')
            this.clearData()
            this.saveLoading = false
          }).catch(() => {
            debugger
            this.saveLoading = false
          })
        }
      }
    },
    closeDialog() {
      this.$emit('closeDialog')
      this.$refs['form'].clearValidate()
    },
    clearData() {
      this.dataForm = {
        id: null,
        startTime: '',
        endTime: '',
        stopProductionReport: null,
        outletId: null,
        remark: null
      }
      this.$refs.uploadFile.initFile()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
